package com.student.foodAfter.controller;

import com.student.foodAfter.common.aop.OperLog;
import com.student.foodAfter.utils.Result;
import com.student.foodAfter.entity.Address;
import com.student.foodAfter.service.AddressService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("address")
public class AddressController {
    @Resource
    private AddressService addressService;

    //得到地址列表
    @GetMapping("/getAddressList/{username}")
    @OperLog(operModul = "地址模块",operType = "查询",operDesc = "查询用户地址")
    public Result getUserAddress(@PathVariable String username){
        Result userAddress = addressService.getUserAddress(username);
        return userAddress;
    }
    //新增地址
    @PostMapping("/addUserAddress")
    @OperLog(operModul = "地址模块",operType = "增加",operDesc = "新增用户地址")
    public Result addUserAddress(@RequestBody Object obj){
        Map entity = (Map)obj;
        //获取object中name的值：
        String name = entity.get("name").toString();
        //获取电话号码
        String phone = entity.get("tel").toString();
        //获得用户名
        String username = entity.get("username").toString();
        //获取邮政编码
        String postalCode = entity.get("postalCode").toString();
//        //获取object中age的值
//        int age = Integer.valueOf(entity.get("age"));
        //省市区
        String province = entity.get("province").toString();
        String city = entity.get("city").toString();
        String county = entity.get("county").toString();
        String isde = entity.get("isDefault").toString();
        // 详细地址
        String addressDetail = entity.get("addressDetail").toString();

        //默认地址
        int isdefault;
        if (isde.equals("true"))
             isdefault=1;
        else
             isdefault=0;

        Address address = new Address();
        address.setName(name);
        address.setPhone(phone);
        address.setCode(postalCode);
        address.setArea(province+"-"+city+"-"+county);
        address.setIsdefault(isdefault);
        address.setUsername(username);
        address.setAddress(addressDetail);
        boolean b = addressService.addUserAddress(address);
        if (b)
            return Result.success("插入成功");
        else
            return Result.fail("插入失败");
    }
    //删除地址
    @PostMapping("/delUserAddress")
    @OperLog(operModul = "地址模块",operType = "删除",operDesc = "删除用户地址")
    public Result delUserAddress(@RequestBody Object obj){
        Map map = (Map) obj;
        String username= map.get("username").toString();
        String name    = map.get("name").toString();
        String phone   = map.get("tel").toString();
        String ssq = map.get("ssq").toString();
        String address = map.get("address").toString();
        boolean b = addressService.delUserAddress(username, name, phone, ssq,address);
        if(b)
            return Result.success("删除成功");
        else
            return Result.fail("删除失败");
    }

    //更改地址默认状态
    @PostMapping("/updateAddressDefault")
    @OperLog(operModul = "地址模块",operType = "修改",operDesc = "修改用户地址默认状态")
    public Result updateAddressDefault(@RequestBody Object obj){
        Map map = (Map) obj;
        String username= map.get("username").toString();
        String name    = map.get("name").toString();
        String phone   = map.get("tel").toString();
        String ssq = map.get("ssq").toString();
        String address = map.get("address").toString();
        boolean b = addressService.modifyAddressDefault(username, name, phone, ssq, address);
        if(b)
            return Result.success("更改成功");
        else
            return Result.fail("更改失败");
    }
}
